package com.realdtech.wechat.task.sms;

import com.jfinal.core.Controller;
import com.jfinal.ext2.kit.RandomKit;
import com.jfinal.kit.HashKit;
import com.jfinal.log.Log;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.MessageFormat;

public class SMS {
	private static final Log log = Log.getLog(SMS.class);
	/**
	 * 短信验证码Cookie名称
	 */
	public static final String CAPTCHA_NAME = "_SMS_CAPTCHA";
	/**
	 * 最后发送时间Cookie名称
	 */
	public static final String CAPTCHA_LAST_SEND_TIME_NAME = "_SMS_CAPTCHA_LAST_SEND_TIME";

	public static void send(String content, String signature, String mobileNos) {

		if (StringUtils.isEmpty(content) || StringUtils.isEmpty(mobileNos)) {
			log.error("发送空内容短信或手机号码列表为空");
			return;
		}
		if (log.isInfoEnabled()) {
			log.info("###############开始发送短信###############");
			log.info("短信内容：" + content);
			log.info("签名：" + signature);
			log.info("手机号：" + mobileNos);
		}
		try {
			content += signature;
			// 创建StringBuffer对象用来操作字符串
			StringBuffer sb = new StringBuffer("http://m.5c.com.cn/api/send/?");
			// APIKEY
			sb.append("apikey=7bfc1b4e472628a0a75cb03ccd628bf5");
			// 用户名
			sb.append("&username=ytbxx");
			// 向StringBuffer追加密码
			sb.append("&password=asdf1234");
			// 向StringBuffer追加手机号码
			sb.append("&mobile=").append(mobileNos);
			// 向StringBuffer追加消息内容转URL标准码
			sb.append("&content=" + URLEncoder.encode(content, "GBK"));
			// 创建url对象
			URL url = new URL(sb.toString());
			// 打开url连接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			// 设置url请求方式 ‘get’ 或者 ‘post’
			connection.setRequestMethod("POST");
			// 发送
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			// 返回发送结果
			String result = in.readLine();
			// 输出结果
			if (log.isInfoEnabled()) {
				log.info("发送结果：" + result);
			}
			in.close();
		} catch (IOException e) {

			log.error("未能成功发送短信。", e);
		}

		if (log.isInfoEnabled()) {
			log.info("###############发送短信结束###############");
		}

	}

	public static void send(String content, String mobileNos) {
		send(content, "【金融链】", mobileNos);
	}

	public static String sendCaptcha(String mobileNos) {
		return sendCaptcha("【金融链】", mobileNos);
	}

	public static String sendCaptcha(String signature, String mobileNos) {

		String content = "尊敬的用户，认证手机验证码：{0}，5分钟内输入有效。金融链工作人员不会索取，请勿泄漏。有问题请致电客服400-606-9989";
		String captcha = RandomKit.smsAuthCode(6);
		content = MessageFormat.format(content, captcha);
		send(content, signature, mobileNos);
		return captcha;
	}

	public static boolean validateCaptcha(Controller controller, String userInputCaptcha) {

		String captcha1 = controller.getCookie(CAPTCHA_NAME);
		captcha1= HashKit.md5(captcha1);
		String captcha2 = userInputCaptcha;
		captcha2= HashKit.md5(captcha2);
		return StringUtils.equalsIgnoreCase(captcha1, captcha2);
	}
}
